/*** Eclipse Class Decompiler plugin, copyright (c) 2016 Chen Chao (cnfree2000@hotmail.com) ***/
package nc.bs.mo.mo2010;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import javax.naming.NamingException;
import nc.bs.logging.Logger;
import nc.bs.ml.NCLangResOnserver;
import nc.bs.mo.mo2010.Disassemble;
import nc.bs.pd.pd3030.RtDMO;
import nc.bs.pub.SystemException;
import nc.vo.mm.pub.FreeItemVO;
import nc.vo.mm.pub.pub1020.RtBItemVO;
import nc.vo.mm.pub.pub1030.PickmHeaderVO;
import nc.vo.mm.pub.pub1030.PickmItemVO;
import nc.vo.mm.pub.pub1030.PickmVO;
import nc.vo.pub.BusinessException;
import nc.vo.pub.lang.UFBoolean;
import nc.vo.pub.lang.UFDate;
import nc.vo.pub.lang.UFDouble;

public class GylxDisassemble extends Disassemble {
	public GylxDisassemble(String pk_corp, String gcbm, String date)
			throws NamingException, SystemException {
		super(pk_corp, gcbm, date);
	}

	public PickmVO disassemble(PickmHeaderVO header) throws BusinessException {
		PickmVO pickm = new PickmVO();
		pickm.setParentVO((PickmHeaderVO) header.clone());

		try {
			UFDate e = new UFDate(
					header.getDate() == null ? header.getLogDate()
							: header.getDate());
			String version = header.getGylxver();
			if (version != null && version.trim().length() == 0) {
				version = null;
			}

			PickmItemVO[] items = this.disGylx(header.getPk_corp(),
					header.getGcbm(), header.getWlbmid(), e,
					header.getJhwgsl(), version, 0, (String) null,
					header.getLylx(), header.getFreeitemVO());
			pickm.setChildrenVO(items);
			((PickmHeaderVO) pickm.getParentVO()).setBomver((String) null);
			return pickm;
		} catch (Exception arg5) {
			Logger.error(arg5.getMessage(), arg5);
			throw new BusinessException(arg5.getMessage());
		}
	}

	private PickmItemVO[] fillProduceInfo(PickmItemVO[] items)
			throws SQLException {
		String sql = "select pk_invmandoc,iscreatesonprodorder,supplytype,issend,bd_produce.pk_stordoc,bd_stordoc.storcode,bd_stordoc.storname,virtualflag,bd_produce.materclass from bd_produce left outer join bd_stordoc on bd_produce.pk_stordoc = bd_stordoc.pk_stordoc where pk_produce = ? ";
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;

		try {
			con = this.getConnection();
			stmt = con.prepareStatement(sql);

			for (int i = 0; i < items.length; ++i) {
				stmt.setString(1, items[i].getPk_produce());
				rs = stmt.executeQuery();
				if (rs.next()) {
					items[i].setPk_invmandoc(rs.getString(1));
					String blly = rs.getString(2);
					blly = blly == null ? "N" : blly;
					items[i].setBlly(blly.equals("Y") ? new Integer(1)
							: new Integer(0));
					int blfs = rs.getInt(3);
					items[i].setBlfs(new Integer(blfs));
					String sfkfl = rs.getString(4);
					items[i].setSfkfl(sfkfl == null ? new UFBoolean(false)
							: new UFBoolean(sfkfl));
					String pk_stordoc = rs.getString(5);
					String ckbm = rs.getString(6);
					String ckmc = rs.getString(7);
					items[i].setCkckid(pk_stordoc);
					items[i].setCkckbm(ckbm);
					items[i].setCkckmc(ckmc);
					String sfxx = rs.getString(8);
					sfxx = sfxx == null ? "N" : sfxx;
					items[i].setZdy1(sfxx);
					int materclass = rs.getInt(9);
					items[i].setAttributeValue("materclass", new Integer(
							materclass));
				}

				rs.close();
			}
		} finally {
			try {
				if (rs != null) {
					rs.close();
				}
			} catch (Exception arg26) {
				;
			}

			try {
				if (stmt != null) {
					stmt.close();
				}
			} catch (Exception arg25) {
				;
			}

			try {
				if (con != null) {
					con.close();
				}
			} catch (Exception arg24) {
				;
			}

		}

		return items;
	}

	private PickmItemVO[] disGylx(String pk_corp, String gcbm,
			String pk_invbasdoc, UFDate rq, UFDouble sl, String version,
			int depth, String gxh, Integer lylx, FreeItemVO freeitemvo)
			throws SystemException, NamingException, BusinessException,
			SQLException {
		try {
			if (depth > this.getRestrick_depth()) {
				throw new BusinessException(NCLangResOnserver.getInstance()
						.getStrByID("50082010", "UPP50082010-000018"));
			} else {
				++depth;
				Vector e = new Vector();
				RtDMO rtDmo = new RtDMO();
				RtBItemVO[] rtbitems = null;
				if (depth == 1) {
					rtbitems = rtDmo.getAllGxhl(pk_corp, gcbm, pk_invbasdoc,
							rq, version, sl, freeitemvo);
				} else {
					rtbitems = rtDmo.getAllGxhl(pk_corp, gcbm, pk_invbasdoc,
							rq, (String) null, sl, freeitemvo);
				}

				PickmItemVO[] items = this.transRtBItemVOToPickmItemVO(
						rtbitems, pk_corp, gcbm, sl, rq, gxh, lylx);
				if (items != null) {
					for (int pickmitems = 0; pickmitems < items.length; ++pickmitems) {
						if (items[pickmitems].getZdy1().equals("Y")
								&& items[pickmitems].getBlly().intValue() == 0) {
							PickmItemVO[] items1 = this.disGylx(pk_corp, gcbm,
									items[pickmitems].getWlbmid(), rq,
									items[pickmitems].getJhcksl(),
									(String) null, depth,
									items[pickmitems].getGxh(), lylx,
									items[pickmitems].getFreeitemVO());
							if (items1 == null) {
								throw new BusinessException(NCLangResOnserver
										.getInstance().getStrByID(
												"50082010",
												"UPP50082010-000019",
												(String) null,
												new String[] { "("
														+ items[pickmitems]
																.getClbm()
														+ ":"
														+ items[pickmitems]
																.getClmc()
														+ ")" }));
							}

							for (int j = 0; j < items1.length; ++j) {
								e.add(items1[j]);
							}
						} else {
							e.add(items[pickmitems]);
						}

						items[pickmitems].setZdy1((String) null);
					}
				}

				PickmItemVO[] arg18 = null;
				if (e.size() > 0) {
					arg18 = new PickmItemVO[e.size()];
					e.copyInto(arg18);
				}

				return arg18;
			}
		} catch (Exception arg17) {
			Logger.error(arg17.getMessage(), arg17);
			throw new BusinessException(arg17.getMessage());
		}
	}

	private PickmItemVO[] fillPickmItemVO(PickmItemVO[] items, Integer lylx)
			throws BusinessException, SQLException {
		if (items == null) {
			return null;
		} else {
			this.fillProduceInfo(items);
			if (items != null) {
				for (int i = 0; i < items.length; ++i) {
					if (lylx.intValue() == 0) {
						items[i].setBlly(new Integer(0));
					}
				}
			}

			return items;
		}
	}

	private PickmItemVO[] transRtBItemVOToPickmItemVO(RtBItemVO[] rtBItemVOs,
			String pk_corp, String gcbm, UFDouble jhsl, UFDate flrq,
			String gxh, Integer lylx) throws BusinessException, SQLException {
		try {
			PickmItemVO[] e = null;
			if (rtBItemVOs == null) {
				return null;
			} else {
				Vector v = new Vector();

				for (int i = 0; i < rtBItemVOs.length; ++i) {
					PickmItemVO bodyVO = new PickmItemVO();
					RtBItemVO rtbitem = rtBItemVOs[i];
					bodyVO.setCrowno(String.valueOf(rtbitem.getHh()));
					bodyVO.setPk_corp(pk_corp);
					bodyVO.setGcbm(gcbm);
					bodyVO.setWlbmid(rtbitem.getWlbmid());
					bodyVO.setGylx(rtbitem.getGyfs() == null ? new Integer(0)
							: rtbitem.getGyfs());
					bodyVO.setPk_produce(rtbitem.getPk_produce());
					bodyVO.setTdbz(new Integer(0));
					bodyVO.setKzbz(new Integer(1));
					bodyVO.setXelwfw((UFDouble) null);
					bodyVO.setClbm(rtbitem.getWlbm());
					bodyVO.setClmc(rtbitem.getWlmc());
					bodyVO.setClgg(rtbitem.getInvspec());
					bodyVO.setClxh(rtbitem.getInvtype());
					bodyVO.setWlth(rtbitem.getWlth());
					bodyVO.setJldwmc(rtbitem.getJldwmc());
					bodyVO.setZxlx(new Integer(0));
					bodyVO.setSfATPjc(new UFBoolean(false));
					bodyVO.setSfdc(rtbitem.getSfdc());
					bodyVO.setSfzl(new UFBoolean(false));
					if (bodyVO.getGylx().intValue() == 0) {
						bodyVO.setDwde(rtbitem.getXhsl().div(rtbitem.getFxsl()));
						bodyVO.setDeyl(bodyVO.getDwde().multiply(jhsl));
						bodyVO.setJhcksl(bodyVO.getDeyl());
					} else {
						bodyVO.setDwde(rtbitem.getXhsl().div(jhsl));
						bodyVO.setDeyl(rtbitem.getXhsl());
						bodyVO.setJhcksl(rtbitem.getXhsl());
					}

					bodyVO.setLjcksl(new UFDouble(0));
					bodyVO.setLjyfsl(new UFDouble(0));
					bodyVO.setGxh(gxh == null ? rtbitem.getGxh() : gxh);
					bodyVO.setFlrq(flrq);
					bodyVO.setGzzxid(rtbitem.getPk_wkid());
					bodyVO.setGzzxbm(rtbitem.getGzzxbm());
					bodyVO.setGzzxmc(rtbitem.getGzzxmc());
					if (rtbitem.getSfdc() != null
							&& rtbitem.getSfdc().booleanValue()) {
						bodyVO.setRkckid(rtbitem.getCkid());
						bodyVO.setRkckbm(rtbitem.getCkbm());
						bodyVO.setRkckmc(rtbitem.getCkmc());
					}

					bodyVO.setTldid(rtbitem.getPk_tldid());
					bodyVO.setTldbm(rtbitem.getTldbm());
					bodyVO.setTldmc(rtbitem.getTldmc());
					bodyVO.setBlfs(rtbitem.getBlfs());
					bodyVO.setStatus(2);
					bodyVO.setAttributeValue("freeitemvalue1",
							rtbitem.getAttributeValue("freeitemvalue1"));
					bodyVO.setAttributeValue("freeitemvalue2",
							rtbitem.getAttributeValue("freeitemvalue2"));
					bodyVO.setAttributeValue("freeitemvalue3",
							rtbitem.getAttributeValue("freeitemvalue3"));
					bodyVO.setAttributeValue("freeitemvalue4",
							rtbitem.getAttributeValue("freeitemvalue4"));
					bodyVO.setAttributeValue("freeitemvalue5",
							rtbitem.getAttributeValue("freeitemvalue5"));
					v.addElement(bodyVO.clone());
				}

				e = null;
				if (v.size() > 0) {
					e = new PickmItemVO[v.size()];
					v.copyInto(e);
				}

				return this.fillPickmItemVO(e, lylx);
			}
		} catch (Exception arg12) {
			Logger.error(arg12.getMessage(), arg12);
			throw new BusinessException(arg12.getMessage());
		}
	}
}